<?php
//

class Actions extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();

        $this->load->library('session');
        $this->load->helper('url');
    }

    public function index()
    {
        if ($this->input->post('action'))
        {
            $action = $this->input->post('action');

            switch ($action)
            {
                case 'login':
                    $this->_login();
                    break;

                default:
                    $this->_redirect();
                    break;
            }
        }
    }

    public function logout()
    {
        $this->session->unset_userdata('login');
        redirect("/index");
    }

    protected function _redirect()
    {
        redirect('/index', 'refresh');
    }

    protected function _login()
    {
        $login = $this->input->post('login');
        $password = $this->input->post('password');

        $this->load->model('login_model');
        $loginRes = $this->login_model->login($login,$password);

        if (!$loginRes)
            $this->_redirect();

        $this->session->set_userdata('login','true');
        $this->session->set_userdata('user', $loginRes);

        redirect('admin');
    }
}